package com.zhouyu.controller;

import com.zhouyu.service.ZhouyuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.awt.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Controller
public class ZhouyuController {

	@Autowired
	private ZhouyuService zhouyuService;

	@GetMapping(value = {"/test1"})
	public Test test(int a) {
		System.out.println(a);
		return new Test();
	}

//	@GetMapping(value = {"/test1"})
//	public String test1(LocalDate date) {
//		System.out.println(date);
//		return zhouyuService.test();
//	}

	@GetMapping(value = "/*")
	public String test1() {
		return zhouyuService.test();
	}

	@InitBinder
	public void init(WebDataBinder binder) {
		LocalDate value = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CustomDateEditor customDateEditor = new CustomDateEditor(format,true);
		binder.registerCustomEditor(Date.class, customDateEditor);
		binder.registerCustomEditor(String.class,
				new StringTrimmerEditor(true));
		binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport(){

			@Override
			public void setAsText(String text) throws IllegalArgumentException {
				if(StringUtils.hasText(text)) {
					setValue(LocalDate.parse(text));
				}
			}
		});
	}

}
